package com.service.config;

import com.common.Common;
import com.common.PageUtils;
import com.dao.ConfigDao;
import com.dao.SysOperateLogDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @Description:
 * @date 2021/4/18 11:24
 */
@Slf4j
@Service
public class ConfigService {

    @Autowired
    private ConfigDao configDao;
    @Autowired
    private SysOperateLogDao sysOperateLogDao;

    public PageInfo<HomeAppConfigModel> applicationList(Long id,String pageNo,Integer pageSize){
        PageHelper.startPage(PageUtils.initPage(pageNo), pageSize);
        List<HomeAppConfigModel> list = configDao.applicationList(id);
        for (HomeAppConfigModel homeAppConfig : list) {
            appCategory(homeAppConfig);
            homeAppConfig.setIsH5APPName(homeAppConfig.getIsH5APP() == 0?"否":"是");
            homeAppConfig.setIsOnlineName(homeAppConfig.getIsOnline() == 0?"否":"是");
        }
        return new PageInfo<>(list);
    };

    public HomeAppConfigModel applicationDetail(Long id){
        return configDao.applicationDetail(id);
    };

    private void appCategory(HomeAppConfigModel homeAppConfig){
        Integer appCategory = homeAppConfig.getAppCategory();
        //0软件 1视频 2音乐 3广告 4书籍
        switch (appCategory){
            case 0:
                homeAppConfig.setAppCategoryName("软件");
                break;
            case 1:
                homeAppConfig.setAppCategoryName("视频");
                break;
            case 2:
                homeAppConfig.setAppCategoryName("音乐");
                break;
            case 3:
                homeAppConfig.setAppCategoryName("广告");
                break;
            case 4:
                homeAppConfig.setAppCategoryName("书籍");
                break;
        }
    }

    public void applicationAdd(HomeAppConfig config,String userId,String username){
        config.setCreateTime(new Date());
        config.setUpdateTime(new Date());
        config.setIsH5APP(config.getIsH5APP() == null?0:config.getIsH5APP());
        configDao.applicationAdd(config);

        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.parseLong(userId));
        sysOperateLog.setUserName(username);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_ADD);
        sysOperateLog.setDescription("新增应用配置");
        sysOperateLog.setDataCcope("新增应用配置");
        sysOperateLogDao.insert(sysOperateLog);
    };

    public void applicationUpdate(HomeAppConfig config,String userId,String username){
        Long id = config.getId();
        if(id != null){
            config.setUpdateTime(new Date());
            configDao.applicationUpdate(config);

            SysOperateLog sysOperateLog = new SysOperateLog();
            sysOperateLog.setUserId(Long.parseLong(userId));
            sysOperateLog.setUserName(username);
            sysOperateLog.setActiveType(Common.ACTIVE_TYPE_UPDATE);
            sysOperateLog.setDescription("修改应用配置");
            sysOperateLog.setDataCcope("修改应用配置");
            sysOperateLogDao.insert(sysOperateLog);
        }
    };

    public void applicationDelete(Long id,String userId,String username){
        configDao.applicationDelete(id);

        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.parseLong(userId));
        sysOperateLog.setUserName(username);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_DELETE);
        sysOperateLog.setDescription("删除应用配置");
        sysOperateLog.setDataCcope("删除应用配置");
        sysOperateLogDao.insert(sysOperateLog);
    };


    public PageInfo<ArticleInfoConfigModel> advisoryList(Integer type,Long id, String pageNo, Integer pageSize){
        PageHelper.startPage(PageUtils.initPage(pageNo), pageSize);
        List<ArticleInfoConfigModel> list = configDao.advisoryList(type,id);
        for (ArticleInfoConfigModel configModel : list) {
            configModel.setStateName(configModel.getState() == 0?"上架":"下架");
        }
        return new PageInfo<>(list);
    };

    @Transactional
    public void advisoryAdd(Integer type,ArticleInfoConfig config,String userId,String username){
        config.setCreateTime(new Date());
        config.setUpdateTime(new Date());
        config.setType(type);
        configDao.advisoryAdd(config);

        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.parseLong(userId));
        sysOperateLog.setUserName(username);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_ADD);
        sysOperateLog.setDescription("新增咨询公告");
        sysOperateLog.setDataCcope("新增咨询公告");
        sysOperateLogDao.insert(sysOperateLog);
    };

    public ArticleInfoConfigModel advisoryDetail(Long id){
        ArticleInfoConfigModel model = configDao.advisoryDetail(id);
        if(model != null){
            model.setStateName(model.getState() == 0?"上架":"下架");
        }
        return model;
    };

    @Transactional
    public void advisoryUpdate(ArticleInfoConfig config,String userId,String username){
        Long id = config.getId();
        if(id != null){
            config.setUpdateTime(new Date());
            configDao.advisoryUpdate(config);

            SysOperateLog sysOperateLog = new SysOperateLog();
            sysOperateLog.setUserId(Long.parseLong(userId));
            sysOperateLog.setUserName(username);
            sysOperateLog.setActiveType(Common.ACTIVE_TYPE_UPDATE);
            sysOperateLog.setDescription("修改咨询公告");
            sysOperateLog.setDataCcope("修改咨询公告");
            sysOperateLogDao.insert(sysOperateLog);
        }
    };

    @Transactional
    public void advisoryDelete(Long id,String userId,String username){
        configDao.advisoryDelete(id);

        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.parseLong(userId));
        sysOperateLog.setUserName(username);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_DELETE);
        sysOperateLog.setDescription("删除咨询公告");
        sysOperateLog.setDataCcope("删除咨询公告");
        sysOperateLogDao.insert(sysOperateLog);
    };



    public PageInfo<CarouselModel> carouselList(Long id, String pageNo, Integer pageSize){
        PageHelper.startPage(PageUtils.initPage(pageNo), pageSize);
        List<CarouselModel> list = configDao.carouselList(id);
        for (CarouselModel carouselModel : list) {
            carouselModel.setStatusName(carouselModel.getStatus() == 0?"可用":"不可用");
        }
        return new PageInfo<>(list);
    };

    public CarouselModel carouselDetail(Long id){
        CarouselModel carouselModel = configDao.carouselDetail(id);
        if(carouselModel != null){
            carouselModel.setStatusName(carouselModel.getStatus() == 0?"可用":"不可用");
        }
        return carouselModel;
    };

    @Transactional
    public void carouselAdd(Carousel carousel,String userId,String username){
        carousel.setCreateTime(new Date());
        carousel.setRemark(StringUtils.isBlank(carousel.getRemark())?"":carousel.getRemark());
        configDao.carouselAdd(carousel);

        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.parseLong(userId));
        sysOperateLog.setUserName(username);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_ADD);
        sysOperateLog.setDescription("新增轮播图");
        sysOperateLog.setDataCcope("新增轮播图");
        sysOperateLogDao.insert(sysOperateLog);
    };

    @Transactional
    public void carouselUpdate(Carousel carousel,String userId,String username){
        Long id = carousel.getIdcarousel();
        if(id != null){
            configDao.carouselUpdate(carousel);

            SysOperateLog sysOperateLog = new SysOperateLog();
            sysOperateLog.setUserId(Long.parseLong(userId));
            sysOperateLog.setUserName(username);
            sysOperateLog.setActiveType(Common.ACTIVE_TYPE_UPDATE);
            sysOperateLog.setDescription("修改轮播图");
            sysOperateLog.setDataCcope("修改轮播图");
            sysOperateLogDao.insert(sysOperateLog);
        }
    };

    @Transactional
    public void carouselDelete(Long id,String userId,String username){
        configDao.carouselDelete(id);

        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.parseLong(userId));
        sysOperateLog.setUserName(username);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_DELETE);
        sysOperateLog.setDescription("删除轮播图");
        sysOperateLog.setDataCcope("删除轮播图");
        sysOperateLogDao.insert(sysOperateLog);
    };
}
